const db = require('../db/index');

// 获取部门的信息的id  参数 id department
exports.getDepartmentMsg = (req, res) => {
  let {
    id,
    department
  } = req.body;
  // 从message表中获取相关部门的信息
  let sql = 'select * from message where message_receipt_object=?';
  db.query(sql, department, (err, results) => {
    if (err) return res.cc(err);
    let messageList = []; // 存放部门信息
    console.log(results);

    results.forEach(item => {
      messageList.push(item.id);
    })
    // 再去更新用户表中read_list read_status数据
    let sql1 = 'update users set read_list=?,read_status=1 where id=?';
    db.query(sql1, [JSON.stringify(messageList), id], (err, results) => {
      if (err) return res.cc(err);
      res.send({
        status: 0,
        id,
        messageList
      })
    })
  })
}

// 获取部门的信息列表
exports.getDepartmentMsgList = (req, res) => {
  let sql = 'select * from message where message_receipt_object=?';
  db.query(sql, req.body.department, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      results
    })
  })
}

// 查询用户的read_list read_status 参数 id
exports.getUserMsg = (req, res) => {
  let sql = 'select read_list,read_status from users where id=?';
  db.query(sql, req.body.id, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      read_list: results[0].read_list,
    })
  })
}

// 当用户点击信息时，更新read_list 中的数据 参数 id readId
exports.updateReadList = (req, res) => {
  let {
    id,
    readId
  } = req.body;
  // 获取用户当前的read_list
  let sql = 'select read_list from users where id=?';
  db.query(sql, id, (err, results) => {
    if (err) return res.cc(err);
    // 将read_list 转化为数组
    let read_list = JSON.parse(results[0].read_list);
    // 遍历去掉readId
    read_list = read_list.filter(item => item != readId)

    // 再重新赋值给read_list
    let sql1 = 'update users set read_list=? where id=?';
    db.query(sql1, [JSON.stringify(read_list), id], (err, results) => {
      if (err) return res.cc(err);
      res.send({
        status: 0,
        message: '更新成功'
      })
    })
  })
}

// 将新发布的信息的id 插入到 read_list中 参数 id message_id
exports.insertReadList = (req, res) => {
  let {
    id,
    department,
    message_id
  } = req.body;
  // 获取用户当前的read_list
  let sql = 'select read_list,read_status from users where department=?';
  db.query(sql, department, (err, results) => {
    if (err) return res.cc(err);
    results.forEach(item => {
      if (item.read_status == 1) {
        let read_list = JSON.parse(item.read_list);
        read_list.push(message_id);
        read_list = JSON.stringify(read_list);
        // 再重新赋值给read_list
        let sql1 = 'update users set read_list=? where id=?';
        db.query(sql1, [read_list, id], (err, results) => {
          if (err) return res.cc(err);
        })
      }
    })
    res.send({
      status: 0,
      message: '更新成功'
    })
  })
}

// 将删除的信息的id 从 read_list中删除 参数 id message_id
exports.deleteReadList = (req, res) => {
  let {
    id,
    department,
    delete_id
  } = req.body;
  // 获取用户当前的read_list
  let sql = 'select read_list,read_status from users where department=?';
  db.query(sql, department, (err, results) => {
    if (err) return res.cc(err);

    results.forEach(item => {
      if (item.read_status == 1) {
        let read_list = JSON.parse(item.read_list);
        read_list = read_list.filter(item => item != delete_id);
        read_list = JSON.stringify(read_list);
        // 再重新赋值给read_list
        let sql1 = 'update users set read_list=? where id=?';
        db.query(sql1, [read_list, id], (err, results) => {
          if (err) return res.cc(err);
        })
      }
    })
    res.send({
      status: 0,
      message: '删除成功'
    })
  })
}